/* eslint-disable */
import { App as VueApp } from 'vue'
import { RouteLocationRaw } from 'vue-router'
import { routes } from '@/router'
import { messages } from '@/i18n'
import './style/common.less'

let instance: any = null // 根组件实例

interface IProps {
  container: HTMLDivElement
  render: (props: IRenderProps) => Promise<VueApp>
}

interface IRenderProps {
  routes: RouteLocationRaw[]
  messages: Record<string, string>
}
async function render(props: IProps) {
  const {
    container,
    render: _render
  } = props
  _render({ routes, messages }).then((app) => {
    instance = app
    instance.mount(container.querySelector('#app'))
  })
}

// webpack打包公共文件路径
if (window.__POWERED_BY_QIANKUN__) {
  __webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__
  if (process.env.NODE_ENV === 'production') {
    __webpack_public_path__ += (process.env.BASE_URL || '').replace(/^\//, '')
  }
}

// 生命周期
export async function bootstrap(): Promise<any> {
  console.log('[vue] vue app bootstraped')
}
export async function mount(props: IProps): Promise<any> {
  console.log('mounted')
  console.log('[vue] props from main framework', props)
  render(props)
}
export async function unmount(): Promise<any> {
  console.log('unmounted')
  instance.unmount()
  instance = null
}
